﻿<rules>
  <rule id="Feed.Core.1001" category="core" target="feed" specificationsection ="2.2.6.2.1" requirementlevel="must" format="atom" metadata="true">
    <description>The &lt;atom:id&gt; element MUST contain the URI that identifies the EntitySet represented by the parent &lt;atom:feed&gt; element.</description>
    <action processor="xslt+rng">
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
        <xsl:template match="/">
          <grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:atom="http://www.w3.org/2005/Atom">
            <start>
              <element>
                <!-- Ignore the feed element. -->
                <anyName />
                <ref name="anyAttributes" />
                <interleave>
                  <!-- Apply XSLT to get URI value using macro. -->
                  <xsl:apply-templates select="*[local-name()='Edmx']"/>
                  <zeroOrMore>
                    <!-- Elements here should not be atom:id. -->
                    <ref name="anyElement_other_than_atom_id" />
                  </zeroOrMore>
                </interleave>
              </element>
            </start>
            <define name="anyContent">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                  <text />
                  <ref name="anyElement" />
                </choice>
              </zeroOrMore>
            </define>
            <define name="anyAttributes">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                </choice>
              </zeroOrMore>
            </define>
            <define name="anyElement">
              <element>
                <anyName />
                <zeroOrMore>
                  <choice>
                    <attribute>
                      <anyName />
                    </attribute>
                    <text />
                    <ref name="anyElement" />
                  </choice>
                </zeroOrMore>
              </element>
            </define>
            <define name="anyElement_other_than_atom_id">
              <element>
                <choice>
                  <anyName>
                    <except>
                      <nsName ns="http://www.w3.org/2005/Atom" />
                    </except>
                  </anyName>
                  <nsName ns="http://www.w3.org/2005/Atom">
                    <except>
                      <name>atom:id</name>
                    </except>
                  </nsName>
                </choice>
                <ref name="anyContent" />
              </element>
            </define>
          </grammar>
        </xsl:template>
        <xsl:template match="*[local-name()='Edmx']" xmlns="http://relaxng.org/ns/structure/1.0" xmlns:atom="http://www.w3.org/2005/Atom">
          <element name="atom:id">
            <choice>
              <value>
                $URI$
              </value>
              <value>
                $URI$/
              </value>
            </choice>
          </element>
        </xsl:template>
      </xsl:stylesheet>
    </action>
    <errormessage>The &lt;atom:id&gt; element MUST contain the URI that identifies the EntitySet represented by the parent &lt;atom:feed&gt; element.</errormessage>
  </rule>
</rules>